//
//  ListenViewController.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/17.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ListenViewController: ListenBaseVC {
    
    var btnTag:Int = 11
    var type:Int = 0
    var page:Int = 1
    var totalPage:Int = 1
    let ListenTableViewCellID = "ListenTableViewCell"
    
    var listModelArr:[ListenListModel] = [ListenListModel]()
   
    lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 44, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 44 - OFFSET_BOTTOM), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.bounces = true
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(ListenTableViewCellID), forCellReuseIdentifier: ListenTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的必听"

        setTopView()
        setUI()
        self.tableView.beginHeadRefresh()
    }
    
    func setTopView() {
        let centre = PlayCentreView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 44))
        centre.delegate = self
        centre.leftBtn.setTitle("历史", for: .normal)
        centre.rightBtn.setTitle("收藏", for: .normal)
        centre.showLeft()
        self.view.addSubview(centre)
    }
    
    func setUI() {
        self.view.addSubview(self.tableView)
        
        self.tableView.initMjFooterWithOffest(0) { (taleView) in
            self.page = self.page + 1
            self.setData()
            
        }
        
        self.tableView.initMjHeaderWithOffest(0) { (tableView) in
            self.listModelArr.removeAll()
            self.page = 1
            self.setData()
        }
    }
    
    func setData() {
        MTProgressHUD.show()
        if self.page <= self.totalPage {
            let para = ["startPoint":self.page,"maxPage":20]
            HomeRequestModel.loadHistoryAndCollectData(programId:[String.getUserId(),self.type],parameters: para) { (success, message, model, page) in
                if success && model != nil {
                    self.totalPage = page!
                    for dic in model! {
                        self.listModelArr.append(dic)
                    }
                }
                self.tableView.reloadData()
                if self.listModelArr.count == 0 {
                    self.tableView.setNoData(nodata: true)
                } else {
                    self.tableView.setNoData(nodata: false)
                }
                MTProgressHUD.hidded(true)
            }
        } else {
            MTProgressHUD.hidded(true)
        }
        self.tableView.endAllRefreshing()
    }
}

// MARK: *******  协议  ********
extension ListenViewController: UITableViewDelegate,UITableViewDataSource, PlayCentreViewDelegate, ListenTableViewCellDelegate {
    func playActionDele(cell: UITableViewCell, paly: Bool) {
        let index:IndexPath = self.tableView.indexPath(for: cell) as! IndexPath
    }
    
    func playCentreViewAction(state: Int) {
        if state == 11 {
            self.type = 0
        } else {
            self.type = 2
        }
        self.page = 1
        self.btnTag = state
        self.listModelArr.removeAll()
        self.tableView.beginHeadRefresh()
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listModelArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 95.5 + SCREEN_SCALE_355X(x: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ListenTableViewCellID) as! ListenTableViewCell
        cell.delegate = self
        if self.listModelArr.count > 0 {
            cell.modelList = self.listModelArr[indexPath.row]
        }
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let playVc = PlayVC()
        playVc.id = self.listModelArr[indexPath.row].id
        ISLOGIN_PUSHVIEWCONTROLLER(playVc)
    }

}
